package testes.examples;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class RectangleOverlap extends JPanel{

      int r1x1,r1x2,r2x1,r2x2 ;

      int r1y1,r1y2,r2y1,r2y2 ;

        int r1width,r2width ;

        int r1height,r2height ;

      public static JButton btn = new JButton("Check");

      public RectangleOverlap(int r1x1,int r1y1,int r1x2,int r1y2,int r2x1,int r2y1,int r2x2,int r2y2){

            this.r1x1=r1x1;

            this.r1x2=r1x2;

            this.r1y1=r1y1;

            this.r1y2=r1y2;

            this.r2x1=r2x1;

            this.r2x2=r2x2;

            this.r2y1=r2y1;

            this.r2y2=r2y2;

            r1width = Math.abs(r1x1-r1x2);

            r2width = Math.abs(r2x1-r2x2);

            r1height = Math.abs(r1y1-r1y2);

            r2height = Math.abs(r2y1-r2y2);

            addActionListener();

      }



      private void addActionListener() {

            btn.addActionListener(new ActionListener(){



                  public void actionPerformed(ActionEvent e) {

                        checkOverlap();

            }

            });



      }

      private void checkOverlap() {

            // condition to check whether the rectangles are overlapping or not.s

            boolean isOVerlap= ((r1x2 >= r2x1) &&

                  (r1y2 >= r2y1) &&

                  (r1x1 <= r2x2) &&

                (r1y1 <= r2y2));



      if(isOVerlap ){

            JOptionPane.showMessageDialog(null, "OVerlap");

      }else{

            JOptionPane.showMessageDialog(null, "No OVerlap");

      }

      }



      @Override

      protected void paintComponent(Graphics g) {

            g.drawRect(r1x1,r1y1 , r1width, r1height);

            g.setColor(new Color(123,232,122));

            g.drawRect(r2x1, r2y1, r2width,r2height);

      }



      public static void main(String args[]){

            JFrame frame = new JFrame();

            frame.setSize(500,500);

// input to check overlap condition.

// the order followed as : enter coordinate for 1st rectangle as lower limit and upper limit to rectangles

            frame.getContentPane().add(new RectangleOverlap(20,30,120,130,100,131,190,150),BorderLayout.CENTER);

            frame.getContentPane().add(btn,BorderLayout.SOUTH);

            frame.addWindowListener(new WindowAdapter(){

                  @Override

                  public void windowClosing(WindowEvent e) {

                        System.exit(0);

                  }



            });

            frame.setVisible(true);

      }

}